//
//  InboxCell.m
//  HotWord
//
//  Created by Ji Qi on 12-6-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "InboxCell.h"

@implementation InboxCell
@synthesize nicknameLable;
@synthesize contentLable;
@synthesize userHeadImageView;
@synthesize isNewMessage;
@synthesize timeLabel;
@synthesize deleteBtn;
@synthesize message;
@synthesize delegate;
@synthesize isDeleting;
@synthesize indexPath;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (NSString *)reuseIdentifier
{
    return @"InboxCell";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)awakeFromNib
{
    UISwipeGestureRecognizer *leftSwipeGestureRecognzer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
    leftSwipeGestureRecognzer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:leftSwipeGestureRecognzer];
    
    UISwipeGestureRecognizer *rightSwipeGestureRecognzer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
    rightSwipeGestureRecognzer.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:rightSwipeGestureRecognzer];
    
    UITapGestureRecognizer *tapHeadGestureRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapHeadGestureRecongnizer.cancelsTouchesInView = YES;
    [self.userHeadImageView addGestureRecognizer:tapHeadGestureRecongnizer];
    
    [self setUserInteractionEnabled:YES];
}

- (void)handleLeftSwipe:(UIGestureRecognizer *)recognizer
{
    self.isDeleting = YES;
    self.width = 380;
    [UIView animateWithDuration:0.3 
                     animations:^{
                         self.left = -60;
                     }];
}

- (void)handleRightSwipe:(UIGestureRecognizer *)recognizer
{
    self.isDeleting = NO;
    self.width = 320;
    [UIView animateWithDuration:0.3 
                     animations:^{
                         self.left = 0;
                     }];
}

- (void)handleTapGesture:(UIGestureRecognizer *)recognizer
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(InboxHeaderDidSelected:)]) {
        [self.delegate InboxHeaderDidSelected:self];
    }
}

- (void)setMessageForCell:(Message *)aMessage
{
    self.message = aMessage;
    NSTimeInterval timestamp = [self.message.timestamp intValue];
    
    self.nicknameLable.text = self.message.dialogNickname;
    self.contentLable.text = self.message.content;
    self.timeLabel.text = [UtilHelper getIntervalString:timestamp];
    [self.userHeadImageView setDefaultImage:[UIImage imageNamed:@"photo_card.png"]];
    [self.userHeadImageView loadImage:self.message.dialogAvator];
    if ([self.message.isReaded boolValue]) {
        self.isNewMessage.hidden = YES;
    }
    else {
        self.isNewMessage.hidden = NO;
    }
}

- (IBAction)tapOnDeleteBtn:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(InboxCellDidDelete:)]) {
        [self.delegate InboxCellDidDelete:self];
    }
}

- (void)dealloc {
    [indexPath release];
    [message release];
    [nicknameLable release];
    [contentLable release];
    [userHeadImageView release];
    [isNewMessage release];
    [timeLabel release];
    [deleteBtn release];
    [super dealloc];
}
@end
